package hms.rooms;

import hms.database.DAO;
import hms.database.DAOnoGUI;
import hms.database.DBRowObject;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

public class RoomType extends DBRowObject {
	
	private String roomTypeLabel;
	private int pricePerNight;
	private char isSmokingAllowed; // 't' or 'f'
	private char isConnectable; // 't' or 'f'
	private String bedSize;
	private int numberOfBeds;
	
	public RoomType(){
		super();
		roomTypeLabel = "Default";
		pricePerNight = 100;
		isSmokingAllowed = 'F';
		isConnectable = 'F';
		bedSize = "Twin";
		numberOfBeds = 2;		
	}
	
	public RoomType(int roomTypeID){
		super(roomTypeID);
	}
	
	public RoomType(String i_roomTypeLabel){
		this();
		setRoomTypeLabel(i_roomTypeLabel);
	}
	
	public RoomType(String i_roomTypeLabel, int i_price, char i_smoking, char i_connectable,
			String i_bedSize, int i_numBeds){
		
		this();
		setRoomTypeLabel(i_roomTypeLabel);
		setPricePerNight(i_price);
		setIsSmokingAllowed(i_smoking);
		setIsConnectable(i_connectable);
		setBedSize(i_bedSize);
		setNumberOfBeds(i_numBeds);
		
	}
	
	public void setRoomTypeLabel(String i){
		roomTypeLabel = i;
		markAsChanged();
	}
	
	public void setPricePerNight(int i){
		pricePerNight = i;
		markAsChanged();
	}
	
	public void setIsSmokingAllowed(char i){
		isSmokingAllowed = i;
		markAsChanged();
	}
	
	public void setIsConnectable(char i){
		isConnectable = i;
		markAsChanged();
	}
	
	public void setBedSize(String i){
		bedSize = i;
		markAsChanged();
	}
	
	public void setNumberOfBeds(int i){
		numberOfBeds = i;
		markAsChanged();
	}
	
	public String getRoomTypeLabel(){
		return roomTypeLabel;
	}
	
	public int getPricePerNight(){
		return pricePerNight;
	}
	
	public char getSmokingAllowed(){
		return isSmokingAllowed;
	}
	
	public char getIsConnectable(){
		return isConnectable;
	}
	
	public String getBedSize(){
		return bedSize;
	}
	
	public int getNumberOfBeds(){
		return numberOfBeds;
	}
	
	public String[] getAmenities(){
		return new String[1];
		// TODO: [MATT] Add way to get amenities from table
	}
	
	@Override
	protected String[] getFieldNames() {

		String[] out = new String[6];
		out[0] = "ROOMTYPELABEL";
		out[1] = "PRICEPERNIGHT";
		out[2] = "ISSMOKINGALLOWED";
		out[3] = "ISCONNECTABLE";
		out[4] = "BEDSIZE";
		out[5] = "NUMBEROFBEDS";
		return out;
	}
	@Override
	protected void loadPreparedStatement(PreparedStatement stmt)
			throws SQLException {
		stmt.setString(1, roomTypeLabel);
		stmt.setInt(2, pricePerNight);
		stmt.setString(3, Character.toString(isSmokingAllowed));
		stmt.setString(4, Character.toString(isConnectable));
		stmt.setString(5, bedSize);
		stmt.setInt(6, numberOfBeds);

		
	}
	@Override
	protected String getUniqueIdentifierFieldName() {
		return "ROOMTYPEID";
	}
	@Override
	protected String getTableName() {
		return "ROOM_TYPE";
	}
	@Override
	protected void loadFromDatabase(ResultSet rs) throws SQLException {
		this.roomTypeLabel = rs.getString("ROOMTYPELABEL");
		this.pricePerNight = rs.getInt("PRICEPERNIGHT");
		this.isSmokingAllowed = rs.getString("ISSMOKINGALLOWED").charAt(0);
		this.isConnectable = rs.getString("ISCONNECTABLE").charAt(0);
		this.bedSize = rs.getString("BEDSIZE");
		this.numberOfBeds = rs.getInt("NUMBEROFBEDS");
	}
	@Override
	protected boolean allowDuplicateRows() {
		return false;
	}
	
	public LinkedList<String> getAmenitiesList(){
		
		DAO dbHandler = DAOnoGUI.getDAO();
		
		LinkedList<String> returnList = new LinkedList<String>();
		
		ResultSet list = dbHandler.getResult(
				"SELECT * FROM INCLUDES_AMENTITIES" +
				" WHERE ROOMTYPELABEL = \'" + roomTypeLabel + "\';");
		
		try {
			while(list.next()){
				returnList.add(list.getString("AMENITY"));
			}
			
		} catch (SQLException e) {
			e.printStackTrace();	
		}

		DAOnoGUI.closeDAO();
		return returnList;
	}
	
	public boolean addAmenityToDatabase(String amenity){
		DAO dbHandler = DAOnoGUI.getDAO();
		
		return dbHandler.updateCommand("INSERT INTO INCLUDES_AMENTITIES" +
				" (ROOMTYPELABEL, AMENITY) VALUES(\'" + roomTypeLabel +
				"\', \'" + amenity + "\');");
	}
	
	@Override
	public String toString(){
		return this.roomTypeLabel;
	}
}
